<?php

namespace app\common\traits;

use think\Db;

trait Provide
{
    /**
     * redis
     * @return \Predis\Client
     */
    public static function redis()
    {
        $client = new \Predis\Client([
            'host'   => '127.0.0.1',
            'port'   => 6379,
        ]);
        return $client;
    }

    /**
     * 获取房间信息
     * @param $room_id
     */
    public static function getRoomInfo( $room_id = null)
    {
        $room_name = 'room_info_'.$room_id;
        $room_info = self::redis()->get($room_name);
        if(!$room_info)
        {
            $room_info = Db::name('room')
                ->alias('r')
                ->join('teacher t','r.teacher_id = t.id')
                ->field('r.*, t.name as teacher_name, t.avatar as teacher_avatar')
                ->where('room_id', $room_id)
                ->find();
            if($room_info)
            {
                self::redis()->set($room_name, json_encode($room_info));
                return $room_info;
            }else{
                return false;
            }
        }else{
            return json_decode($room_info ,true);
        }
    }

    /*
     * 获取文章分类
     */
    public function getArticleCate($state = 1)
    {
        return Db::name('article_cate')->where('state', $state)->cache()->select();
    }

    /*
     * 获取老师列表
     */
    public function getTeacherList($state = 1)
    {
        return Db::name('teacher')->where('state', $state)->cache()->select();
    }

    /**
     * 更改房间信息
     */
    public static function editRoomInfo()
    {

    }

    /**
     * 获取用户信息
     */
    public static function getUserInfoByToken($token)
    {
        $info = self::redis()->get($token);
        if($info)
        {
            return json_decode($info, true);
        }else{
            return false;
        }
    }

    /**
     * 获取token
     */
    public static function getToken($user_info)
    {
        $expire_time = 86400 * 16;
        $token = md5($user_info['user_id'].$user_info['room_id'].time()); //生成token
        self::redis()->set($token, json_encode($user_info));
        //设置失效时间为15天
        self::redis()->expire($token, $expire_time);
        return $token;
    }

    /**
     * 获取房间缓存消息
     * $room_id 房间ID
     * $num     获取条数默认50条
     * $is_examine 是否过滤审核  1 返回审核后的 0 全部返回
     * $msg_state  消息状态 1正常 0撤回
     */
    public static function getRedisMessage($room_id = null, $star = 0, $end = -1,$is_examine = 0)
    {
        if(!$room_id)return false;
        $list = self::redis()->lRange($room_id, $star, $end);
        if($is_examine)
        {
            foreach ($list as $key => $val)
            {
                $list[$key] = json_decode($val, true);
                if($list[$key]['is_examine'] == 0 || $list[$key]['msg_state'] == 0)
                {
                    unset($list[$key]);
                }
            }
        }else{
            foreach ($list as $key => $val)
            {
                $list[$key] = json_decode($val, true);
                if($list[$key]['msg_state'] == 0)
                {
                    unset($list[$key]);
                }
            }
        }
        return $list;
    }

    /**
     * 修改用户状态
     */
    public static function editUserState($user_id , $stat = 1)
    {
        return Db::name('user')->where('id', $user_id)->edit(['state'=>$stat]);
    }

    /**
     * 获取随机用户ID
     */
    public static function getUserID()
    {
        return self::getMillisecond().mt_rand(1111,9999);
    }

    /**
     * 获取毫秒时间戳
     */
    public static function getMillisecond(){
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectimes = substr($msectime,0,13);
    }

    /**
     * 获取随机ID
     */
    public static function getRandomId()
    {
        return self::getMillisecond().mt_rand(1111,9999);
    }

    /**
     * 获取游客随机名字
     */
    public static function getTouristName()
    {
        return '游客'.randomString(6);
    }

    /**
     * 获取头像
     */
    public static function getAvatar($type)
    {
        switch ($type)
        {
            //游客
            case '1':
                return '/static/images/tourist.png';
            default:
                return '/static/images/tourist.png';
        }
    }
    /*
     * 获取角色icon
     */
    public static  function getRoleIcon($role)
    {
        switch ($role)
        {
            //游客
            case 1:
                return '/static/images/youke.png';
            //会员
            case 2:
                return '/static/images/huiyuan.png';
            //讲师
            case 3:
                return '/static/images/teacher.png';
            //助理
            case 4:
                return '/static/images/kefu.png';
            //管理
            case 5:
                return '/static/images/power.png';
            default:
                return '/static/images/youke.png';
        }
    }

    /**
     * @param $arr 数组去重
     * @param $key
     * @return mixed
     */
    public static function array_unset($arr, $key){
        $tmp_arr = array();
        foreach($arr as $k => $v)
        {
            if(in_array($v[$key], $tmp_arr))
            {
                unset($arr[$k]);
            }
            else {
                $tmp_arr[$k] = $v[$key];
            }
        }
        return $arr;
    }
}